/*
 * File: exercise6.cpp
 * -------------------
 * This program finds the largest integer and the second largest integer in a list.
 * compile command :
 * g++ -Wall -g exercise6.cpp -o exercise6 -lStanfordCPPLib -I ../include/
 */

#include <iostream>
#include "simpio.h"

using namespace std;

const int SENTINEL = 0;

int main()
{
	int large ;
	int second ;
	int i;
	cout<<"This program finds the largest integer and the second largest integer in a list."<<endl;
	cout<<"Enter "<<SENTINEL<<" to signal the end of the list."<<endl;
	i = getInteger("? ");
	large = i;
	second = i;
	if(i == SENTINEL)
	{
		cout<<"You just enter: "<<SENTINEL<<endl;
		return 0;
	}
	i = getInteger("? ");
	if(i == SENTINEL)
	{
		if(i >= large)
		{
			large = i;
			cout<<"The largest value was "<<large<<endl;
			cout<<"The second largest value was "<<second<<endl;
		}
		else
		{
			second = i;
			cout<<"The largest value was "<<large<<endl;
			cout<<"The second largest value was "<<second<<endl;
		}
		return 0;
	}
	else
	{
		if(i >= large)
		{   
			large = i;
		}
		else
		{
			second = i;
		}
	}

	while((i = getInteger("? ")) != SENTINEL)
	{
		if(i > large)
		{
			second = large;
			large = i;
		}
		else if(i > second)
		{
			second = i;
		}
	}
	cout<<"The largest value was "<<large<<endl;
	cout<<"The second largest value was "<<second<<endl;
	return 0;
}
